package com.syq;

import com.syq.utils.EsUtil;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.io.IOException;

/**
 * @ClassName: test
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/5 16:06
 */


public class testEs {

    private String index="person";

    //链接
    @Test
    public void test1() throws Exception {
        //使用Java链接ES服务器
        RestHighLevelClient client = EsUtil.getClient();
        //创建索引库
        IndicesClient indices = client.indices();
        //构造器放入索引库名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        String json="{\n" +
                "    \"number_of_shards\": 5,\n" +
                "    \"number_of_replicas\": 1\n" +
                "  }";
        createIndexRequest.settings(json, XContentType.JSON);
/*        String mapping="{\n" +
                "    \"properties\": {\n" +
                "      \"name\":{\n" +
                "        \"type\": \"text\", \n" +
                "        \"analyzer\": \"ik_max_word\",\n" +
                "        \"index\": true,\n" +
                "        \"store\": true\n" +
                "      },\n" +
                "      \"auth\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"count\":{\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"createtime\":{\n" +
                "        \"type\": \"date\"\n" +
                "        , \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
                "      },\n" +
                "      \"desc\":{\n" +
                "        \"type\": \"text\"\n" +
                "        , \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }";
        createIndexRequest.mapping(mapping,XContentType.JSON);*/
        XContentBuilder mapping = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("name")
                .field("type", "text")
                .endObject()
                .startObject("age")
                .field("type", "integer")
                .endObject()
                .startObject("birthday")
                .field("type", "date")
                .field("format", "yyyy-MM-dd")
                .endObject()
                .endObject()
                .endObject();
        createIndexRequest.mapping(mapping);
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        if(!createIndexResponse.isAcknowledged()){
            System.out.println("创建索引失败"+createIndexResponse);
        }
        System.out.println("创建索引成功"+createIndexResponse);
        //操作文档
        //IndexResponse index = client.index();

    }

    @Test
    public void testIndex() throws Exception{
        RestHighLevelClient client = EsUtil.getClient();
        IndicesClient indices = client.indices();
        GetIndexRequest getIndexRequest = new GetIndexRequest(index);
        boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT);
        if (exists){
            System.out.println(index+"存在");
        }
    }

    @Test
    public void testDelete() throws Exception{
        RestHighLevelClient client = EsUtil.getClient();
        IndicesClient indices = client.indices();
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("sms-logs-index");
        AcknowledgedResponse delete = indices.delete(deleteIndexRequest, RequestOptions.DEFAULT);

    }
}
